package com.eastfair.home.service.impl;

import com.eastfair.core.base.R;
import com.eastfair.core.context.StaticContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.home.service.HomeHomeBondManageService;
import com.eastfair.home.service.HomeServiceProviderService;
import com.eastfair.messagecenter.util.SendMsgUtil;
import com.eastfair.venuebooking.api.HomeBondManageFeign;
import com.eastfair.venueservice.dto.BondManageDTO;
import com.eastfair.venueservice.vo.ServiceProviderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 主场保证金 service.
 *
 * @author dqq
 * @date 2023 /4/18
 */
@Service
@Slf4j
public class HomeHomeBondManageServiceImpl implements HomeHomeBondManageService {

    @Resource
    private HomeBondManageFeign homeBondManageFeign;

    @Resource
    private HomeServiceProviderService homeServiceProviderService;

    @Resource
    private SendMsgUtil sendMsgUtil;

    @Override
    public boolean submit(BondManageDTO bondManageDTO) {
        log.info("submit - 提交保证金订单, bondManageDTO={}", bondManageDTO);
        R<Boolean> result = homeBondManageFeign.submit(bondManageDTO);
        log.info("submit - 提交保证金订单, result={}", result);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public boolean sendConfigPayeeInnerMsg(BondManageDTO bondManageDTO) {
        log.info("sendConfigPayeeInnerMsg - 发送配置收款商户的站内信, bondManageDTO={}", bondManageDTO);
        String orderType = "保证金订单";
        ServiceProviderVO home = homeServiceProviderService.getHomeByExhibitionIdAndSpaceCode(bondManageDTO.getExhibitionManageId(), bondManageDTO.getSpaceCode());
        if (home == null) {
            log.warn("无法发动站内信，未找到主场服务商");
            return false;
        }
        String content = String.format("主场【%s】，您好，%s已生成，如需收款请到财务系统配置收款账号！", home.getCompanyName(), orderType);
        sendMsgUtil.sendInnerMsgByTeamId(content, home.getId(), StaticContextUtil.getProjectSystemScene());
        return true;
    }
}